﻿$(function () {
    layui.use(['element', 'notice', 'layer', 'tree', 'table', 'iziToast'], function () {
        var layer = layui.layer,
            element = layui.element,
            tree = layui.tree,
            table = layui.table,
            notice = Util.notice !== null && Util.notice() !== null
                ? Util.notice() : layui.notice,
            iziToast = layui.iziToast;
        let initSystemModuleTree = function (tableObj) {
            let $treeContainer = $("div.systemmodulemanage .layui-nav-custom-content");
            httpapi.fetchGet('/api/system/getsystemmoduletreeall').then(res => {
                if (!httpapi.checkResponse(res))
                    //输入信息校验错误
                    notice.error(res.message);
                else {//Success
                    tree.render({
                        elem: $treeContainer,
                        id: "systemmoduletree",
                        onlyIconControl: true,
                        data: res.data,
                        click: function (obj) {
                            if (obj.data.id) {
                                if (moduletableconfig.url.indexOf('?') >= 0) {
                                    let arr_url = moduletableconfig.url.split('?');
                                    moduletableconfig.url = arr_url[0];
                                }
                                moduletableconfig.url += '?id=' + obj.data.id;
                                tableObj.reload(moduletableconfig);
                            }
                        }
                    });
                }
            }).catch(error => notice.error("对不起，出现未知错误，请您联系系统管理员！"));
        },
        initSystemModuleTable = function (tableconfig) {
            return table.render(tableconfig);
        };

        let tableIns = initSystemModuleTable(moduletableconfig);
        initSystemModuleTree(tableIns);

        //监听表格数据行按钮
        let editurl = "/system/systemmodule/edit",
            editWidowConfig = {
                title: "编辑/修改",
                type: 2,
                content: "",
                fixtolayoutbody: true,
                anim: 3
                //width: 720,
                //height: 850
            };
        table.on('tool(systemmoduletable)', function (obj) {
            var data = obj.data,
                thisElem = this,
                buttonid = layui.$(thisElem).data("buttonid");
            if (obj.event === 'Edit') {
                editWidowConfig.content = editurl + "?buttonid=" + buttonid + "&moduleid=" + data.Id;
                Util.OpenMainWindow(editWidowConfig);
            } else if (obj.event === 'Delete') {
                layer.confirm("确定要进行删除操作吗？", function (confirmIndex) {
                    layer.close(confirmIndex);
                    iziToast.loading("删除中，请稍候...");  
                    let node = new Object(DeleteNodeModel);
                    node.EntityId = data.Id;
                    node.ButtonId = buttonid;
                    $.deleteJSON("/api/system/systemmodule/delete", node, function (res) {
                        iziToast.destroy();
                        if (!httpapi.checkResponse(res)) {
                            //输入信息校验错误
                            notice.error(res.message);
                        } else {//Success
                            iziToast.loading(res.message);
                            $.wait(2000).then(function () {
                                Util.MainWindowRedirect("/backend");
                            });
                        }
                    });
                });
            }
        });
        //监听顶部按钮栏
        var PageButtonEvent = {
            Add: function (buttonid) { 
                editWidowConfig.content = editurl + "?buttonid=" + buttonid;
                Util.OpenMainWindow(editWidowConfig);
                return false;
            }
        };
        $('.pagebuttongroup .layui-btn').on('click', function (e) {
            e.preventDefault();
            var type = $(this).data('type');
            var buttonid = $(this).data("buttonid");
            PageButtonEvent[type] ? PageButtonEvent[type].call(this, buttonid) : '';
        });
    });//layui.use([
}); //$(function () {